/**
 * 
 */
package com.idea.auction.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.idea.auction.model.Dictionary;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;
import com.jfinal.server.BaseService;

/**
 * @author y.z
 */
public class DictionaryService extends BaseService<Dictionary> {

	
	/**
	 * 获取父级字典分页
	 * @param curPage
	 * @return
	 */
	public Page<Dictionary> getPage(int curPage){
		return super.getPage(curPage, "select *", " from sys_dictionary where dic_parent_id is NULL order by create_date desc ");
	}
	
	/**
	 * 获取子级字典分页
	 * @param curPage
	 * @param parentId
	 * @return
	 */
	public Page<Dictionary> getchildrePage(int curPage,int parentId){
//		Cache dicCache = Redis.use("dicCache");
//		Map<String, Object> indexMap = dicCache.get(curPage);
//		System.out.println(indexMap);
//		if(null == indexMap){
//			indexMap = new HashMap<String, Object>();
//			indexMap.put("dicPage", super.getPage(curPage, "select *", " from sys_dictionary where dic_parent_id = "+parentId+" order by dic_value,create_date desc "));
//			dicCache.set(curPage, indexMap);
//		}
		
		
		return super.getPage(curPage, "select *", " from sys_dictionary where dic_parent_id = "+parentId+" order by dic_value,create_date desc ");
	}
	
	/**
	 * 保存数据字典
	 * @param dictionary
	 * @return
	 */
	public boolean add(Dictionary dictionary){
		return super.save(dictionary);
	}
	
	/**
	 * 更新数据字典
	 * @param dictionary
	 * @return
	 */
	public boolean update(Dictionary dictionary){
		return super.update(dictionary);
	}
	
	/**
	 * 根据id查询详情
	 * @param id
	 * @return
	 */
	public Dictionary getById(int id){
		return super.findById(id);
	}
	
	/**
	 * 删除数据字典
	 * @param id
	 * @return
	 */
	public boolean del(int id){
		boolean flag = false;
		
		if(null != super.findFirst("select * from sys_dictionary where dic_parent_id = "+id+"")){
			
			if(super.update("delete from sys_dictionary where dic_parent_id = "+id+"")){
				
				if(super.delById(id)){
					flag = true;
				}
			}
		}else{
			flag = super.delById(id);
		}
		
		return flag;
	}
	
	/**
	 * 验证唯一标识
	 * @param code
	 * @return
	 */
	public boolean validateCode(String code){
		boolean flag = false;
		
		if(null == super.findFirst("select * from sys_dictionary where dic_code = '"+code+"'")){
			flag = true;
		}
		
		return flag;
		
	}
	
	/**
	 * 验证值是否存在
	 * @param value
	 * @param parentId
	 * @return
	 */
	public boolean validateValue(String value,String parentId,String id){
		boolean flag = false;
		String sql = "select * from sys_dictionary where dic_value = '"+value+"' and dic_parent_id = "+parentId+"";
		
		if(!StrKit.isBlank(id)){
			sql = "select * from sys_dictionary where dic_value = '"+value+"' and dic_parent_id = "+parentId+" and id != "+id+"";
		}
		
		
		if(null == super.findFirst(sql)){
			flag = true;
		}
		
		return flag;
	}
	
	/**
	 * 根据code查询/
	 * @param dic_code
	 * @return
	 */
	public List<Dictionary> getListByCode(String dicCode){
		return super.find("select dic_value,dic_name from sys_dictionary where dic_parent_id=(select id from sys_dictionary where dic_code='"+dicCode+"') order by dic_value");
	}
	
	/**
	 * 根据code和value获得值
	 * @param dicCode
	 * @param value
	 * @return
	 */
	public String getName(String dicCode,String value){
		return super.findFirst("select dic_name from sys_dictionary where dic_parent_id=(select id from sys_dictionary where dic_code='"+dicCode+"') and dic_value= '"+value+"' order by dic_value").getStr("dic_name");
	}
	
}
